package com.yimi.appstore.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.yimi.appstore.R;
import com.yimi.appstore.util.AsyncPhoneLoader;
import com.yimi.appstore.util.AsyncPhoneLoader.ImageCallback;

public class GalleryAdapter extends BaseAdapter {

    private Context mContext;

    private int mGalleryBackground;

    private ArrayList<String> imgArray;

    private int mWidth;

    private int mHeight;

    private AsyncPhoneLoader phoneLoader;

    public GalleryAdapter(Context context, ArrayList<String> arr, int width, int height) {
        mContext = context;
        imgArray = arr;
        mWidth = width;
        mHeight = height;
        TypedArray array = mContext.obtainStyledAttributes(R.styleable.Gallery);
        mGalleryBackground = array.getResourceId(R.styleable.Gallery_android_galleryItemBackground,
                0);
        array.recycle();
        phoneLoader = new AsyncPhoneLoader();
    }

    @Override
    public int getCount() {
        return imgArray.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ImageView imageView = (ImageView)convertView;
        if (imageView == null) {
            imageView = new ImageView(mContext);
            try {
                String url = imgArray.get(position);
                imageView.setTag(url);
                Drawable drawable = phoneLoader.loadDrawable(url, mContext, false,
                        new ImageCallback() {

                            @Override
                            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                                ImageView imageViewByTag = (ImageView)parent
                                        .findViewWithTag(imageUrl);
                                if (imageViewByTag != null) {
                                    if (imageDrawable != null) {
                                        imageViewByTag.setImageDrawable(imageDrawable);
                                    } else {
                                        imageViewByTag.setImageResource(R.drawable.photo_empty);
                                    }
                                }
                            }
                        });
                if (drawable == null) {
                    imageView.setImageResource(R.drawable.photo_load);
                } else {
                    imageView.setImageDrawable(drawable);
                }
            } catch (Exception e) {
                e.printStackTrace();
                imageView.setImageResource(R.drawable.icon_empty);
            }

            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(mWidth, mHeight));
            imageView.setBackgroundResource(mGalleryBackground);
        }
        return imageView;
    }

    public float getScale(boolean focused, int offset) {
        return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
    }

}
